package com.hksj.cbm.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hksj.cbm.dto.AlarmDTO;
import com.hksj.cbm.dto.AlarmPageDTO;
import com.hksj.cbm.mapper.AlarmMapper;
import com.hksj.cbm.vo.AlarmNumRankingVO;
import com.hksj.cbm.vo.AlarmRankingVO;
import com.hksj.cbm.vo.AlarmVO;
import com.hksj.cbm.vo.SpaceModelVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author fjt
 * @data 2024/7/15 14:32
 * @describe 报警
 */
@Service
public class AlarmService {


    @Autowired
    private AlarmMapper alarmMapper;
    @Autowired
    private DeviceInstanceService deviceInstanceService;


    /**
     * 报警记录Excel导出提供数据
     * @param dto
     * @return
     */
    public List<AlarmVO> list(AlarmDTO dto){
        Assert.notNull(dto.getCode(), "专业code不能为空");
        List<AlarmVO> voList = alarmMapper.list(dto);
        if (CollUtil.isEmpty(voList)){
            return null;
        }
        // 设置每个设备的空间位置节点信息
        List<String> pathList = voList.stream().map(AlarmVO::getPaths).collect(Collectors.toList());
        if (CollUtil.isEmpty(pathList)){
            return voList;
        }
        Map<String, List<SpaceModelVO>> pathNameMap = deviceInstanceService.devicePathConvertToUTF8(pathList);
        if (CollUtil.isNotEmpty(pathNameMap)){
            voList.forEach(alarmVO -> {
                alarmVO.setSpaceModelVOList(pathNameMap.get(alarmVO.getPaths()));
            });
        }
        return voList;
    }


    /**
     * 报警记录分页查询
     * @param dto
     * @return
     */
    public IPage<AlarmVO> pageAlarm(AlarmPageDTO dto){
        Assert.notNull(dto.getCode(), "专业code不能为空");
        Page<AlarmVO> page = alarmMapper.page(dto, new Page<>(dto.getPageNum(), dto.getPageSize()));
        // 设置每个设备的空间位置节点信息
        List<AlarmVO> voList = page.getRecords();
        if (CollUtil.isEmpty(voList)){
            return page;
        }

        List<String> pathList = voList.stream().map(AlarmVO::getPaths).collect(Collectors.toList());
        if (CollUtil.isEmpty(pathList)){
            return page;
        }
        Map<String, List<SpaceModelVO>> pathNameMap = deviceInstanceService.devicePathConvertToUTF8(pathList);
        if (CollUtil.isNotEmpty(pathNameMap)){
            voList.forEach(alarmVO -> {
                alarmVO.setSpaceModelVOList(pathNameMap.get(alarmVO.getPaths()));
            });
        }
        return page;
    }


    /**
     *  报警次数
     * @param dto
     * @return
     */
    public Integer total(AlarmPageDTO dto){
        Assert.notNull(dto.getCode(), "专业code不能为空");
        return alarmMapper.total(dto);
    }


    /**
     * 批量查询指定的设备，指定的报警类型，当前是否处于报警中
     * @return 处于机组故障报警中的设备id
     */
    public List<String> getUnitMalfunction(List<String> deviceIdList, Integer alarmType){
        return alarmMapper.getUnitMalfunction(deviceIdList, alarmType);
    }

    /**
     * 报警记录排名(前10)
     * @param dto
     * @return
     */
    public List<AlarmRankingVO> ranking(AlarmPageDTO dto){
        Assert.notNull(dto.getCode(), "专业code不能为空");
        List<AlarmRankingVO> voList = alarmMapper.ranking(dto);
        if (CollUtil.isEmpty(voList)){
            return null;
        }
        List<String> pathList = voList.stream().map(AlarmRankingVO::getPaths).collect(Collectors.toList());
        if (CollUtil.isEmpty(pathList)){
            return voList;
        }
        Map<String, List<SpaceModelVO>> pathNameMap = deviceInstanceService.devicePathConvertToUTF8(pathList);
        if (CollUtil.isNotEmpty(pathNameMap)){
            voList.forEach(alarmRankingVO -> {
                alarmRankingVO.setSpaceModelVOList(pathNameMap.get(alarmRankingVO.getPaths()));
            });
        }
        return voList;
    }


    /**
     * 报警记录数量排名(针对上面ranking()方法改造，数据库表结构后期调整了，但是上面方法其他地方有在用不能直接改)
     * @param dto
     * @return
     */
    public List<AlarmNumRankingVO> alarmNumRanking(AlarmPageDTO dto, Integer top){
        Assert.notNull(dto.getCode(), "专业code不能为空");
        return alarmMapper.alarmNumRanking(dto, top);
    }



}
